"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : rename table语法功能不同兼容库验证
Create At   : 2023/02/25
Owner       : @peilinqian
Description :
    step1:创建普通库（默认兼容A库）;
    step2:普通库下创建表;
    step3:普通库进行rename table操作;
    step4:创建兼容B库;
    step5:兼容B库库下创建表;
    step6:兼容B库库进行rename table操作;
    step7:兼容B库库进行rename table操作后查询新表名;
    step8:兼容B库库进行rename table操作后查询原表名;
Expect      :
    step1:创建普通库（默认兼容A库）; expect:创建成功
    step2:普通库下创建表; expect:创建成功
    step3:普通库进行rename table操作; expect:失败报错
    step4:创建兼容B库; expect:创建成功
    step5:兼容B库库下创建表; expect:创建成功
    step6:兼容B库库进行rename table操作; expect:成功
    step7:兼容B库库进行rename table操作后查询新表名; expect:成功
    step8:兼容B库库进行rename table操作后查询原表名; expect:失败
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ClientConnection(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"----{os.path.basename(__file__)} start----")
        self.common = Common()
        self.constant = Constant()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_a_name = 'db_renametable_0001_a'
        self.db_b_name = 'db_renametable_0001_b'
        self.t_name1 = 't_renametable_0001_1'
        self.t_name2 = 't_renametable_0001_2'
        self.t_name3 = 't_renametable_0001_3'
        self.err_flag1 = 'ERROR:  rename table syntax is ' \
                         'supported on dbcompatibility B.'
        self.err_flag2 = f'ERROR:  relation "{self.t_name1}" does not exist'

    def test_set_password(self):
        step_txt = '----step1:创建普通库（默认兼容A库）; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop database if exists {self.db_a_name};" \
            f"create database {self.db_a_name};"
        create_result = self.sh_primary.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_result,
                      '执行失败' + step_txt)

        step_txt = '----step2:普通库下创建表; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name1};" \
            f"create table {self.t_name1}(a int,b int);"
        create_result = self.sh_primary.execut_db_sql(create_sql,
                                                      dbname=self.db_a_name)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_result,
                      '执行失败' + step_txt)

        step_txt = '----step3:普通库进行rename table操作; expect:失败报错----'
        self.log.info(step_txt)
        rename_sql = f"rename table {self.t_name1} to {self.t_name3};"
        rename_result = self.sh_primary.execut_db_sql(rename_sql,
                                                      dbname=self.db_a_name)
        self.log.info(rename_result)
        self.assertIn(self.err_flag1, rename_result, '执行失败' + step_txt)

        step_txt = '----step4:创建兼容B库; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop database if exists {self.db_b_name};" \
            f"create database {self.db_b_name} dbcompatibility='B';"
        create_result = self.sh_primary.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_result,
                      '执行失败' + step_txt)

        step_txt = '----step5:兼容B库库下创建表; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name1};" \
            f"create table {self.t_name1}(a int,b int);"
        create_result = self.sh_primary.execut_db_sql(create_sql,
                                                      dbname=self.db_b_name)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_result,
                      '执行失败' + step_txt)

        step_txt = '----step6:兼容B库库进行rename table操作; expect:成功----'
        self.log.info(step_txt)
        rename_sql = f"rename table {self.t_name1} to {self.t_name3};"
        rename_result = self.sh_primary.execut_db_sql(rename_sql,
                                                      dbname=self.db_b_name)
        self.log.info(rename_result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, rename_result,
                      '执行失败' + step_txt)

        step_txt = '----step7:兼容B库库进行rename table操作后查询新表名; expect:成功----'
        self.log.info(step_txt)
        select_sql = f"select * from {self.t_name3};"
        select_result = self.sh_primary.execut_db_sql(select_sql,
                                                      dbname=self.db_b_name)
        self.log.info(select_result)
        self.assertIn('0 rows', select_result, '执行失败' + step_txt)

        step_txt = '----step8:兼容B库库进行rename table操作后查询原表名; expect:失败----'
        self.log.info(step_txt)
        select_sql = f"select * from {self.t_name1};"
        select_result = self.sh_primary.execut_db_sql(select_sql,
                                                      dbname=self.db_b_name)
        self.log.info(select_result)
        self.assertIn(self.err_flag2, select_result, '执行失败' + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step_txt1 = '----删除普通库及B库表; expect:删除成功----'
        self.log.info(step_txt1)
        drop_sql = f"drop table if exists {self.t_name1};"
        drop_result1 = self.sh_primary.execut_db_sql(drop_sql,
                                                     dbname=self.db_a_name)
        self.log.info(drop_result1)
        drop_sql = f"drop table if exists {self.t_name3};"
        drop_result2 = self.sh_primary.execut_db_sql(drop_sql,
                                                     dbname=self.db_b_name)
        self.log.info(drop_result2)

        step_txt2 = '----删除普通库及B库; expect:删除成功----'
        self.log.info(step_txt2)
        drop_sql = f"drop database if exists {self.db_a_name};" \
            f"drop database if exists {self.db_b_name};"
        drop_result3 = self.sh_primary.execut_db_sql(drop_sql)
        self.log.info(drop_result3)

        self.log.info('----清理环境断言----')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_result1,
                      '执行失败' + step_txt1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_result2,
                      '执行失败' + step_txt1)
        self.assertEqual(drop_result3.count(
            self.constant.DROP_DATABASE_SUCCESS), 2, '执行失败' + step_txt2)

        self.log.info(f'----{os.path.basename(__file__)} end----')
